package com.hangxingliu.exceltools;

import java.awt.FileDialog;
import java.awt.Frame;

public class Utils {

	public static String columnInt2Str(int column) {
		String result = "";
		do {
			result = Character.toString((char)((column % 26) + 'A')) + result;
			column /= 26;
		} while(column > 0);
		return result;
	}

	public static int columnStr2Int(String column) {
		int result = 0;
		while(column.length() > 0) {
			result = result * 26 + (column.charAt(0) - 'A');
			column = column.substring(1);
		}
		return result;
	}

	public static String showExcelPicker4Load(Frame parent) {
		return showExcelPicker(parent, false, null);
	}
	public static String showExcelPicker4Save(Frame parent, String defaultFileName) {
		return showExcelPicker(parent, true, defaultFileName);
	}

	private static String showExcelPicker(Frame parent, boolean forSave, String fileName) {
		String result = null;
		FileDialog dialog = new FileDialog(parent);
		if(forSave) {
			dialog.setMode(FileDialog.SAVE);
			dialog.setFile(fileName);
		} else {
			dialog.setMode(FileDialog.LOAD);
		}
		dialog.setVisible(true);
		String file = dialog.getFile();
		if(file != null)
			result = dialog.getDirectory() + file;
		dialog.dispose();
		return result;
	}
}
